package cn.shop.cloud.web.controller;

import cn.shop.cloud.common.Result;
import cn.shop.cloud.domain.User;
import cn.shop.cloud.resolver.RedisValue;
import cn.shop.cloud.service.UserService;
import cn.shop.cloud.util.CookieUtil;
import cn.shop.cloud.web.util.MD5Util;
import cn.shop.cloud.web.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @program: shop-parent
 * @ClassName TokenController
 * @description:
 * @author: Mokio
 * @create: 2024−12-24 11:35
 * @Version 2024 2.2 ; jdk1.8.0_181
 **/
@RestController
@RequestMapping("/token")
public class TokenController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Result<String> login(@Valid LoginVo loginVo, HttpServletResponse response){

        //登录方法
        String token= userService.login(loginVo);
        //把信息添加到 cookie中   分布式session ===》  redis + token
        CookieUtil.addCookie(response,CookieUtil.TOKEN_COOKIE_NAME,token,CookieUtil.TOKEN_EXPIRE_TIME);
        return Result.success(null);
    }

    public static void main(String[] args) {
        String admin = MD5Util.encode("admin", "1a2d3c4d");
        System.out.println(admin);
    }

    @RequestMapping("/getCurrent")
    public Result<User> getCurrent(@RedisValue User user){

        System.out.println("------------------------");
        System.out.println("------------------->"+user);
        return  Result.success(user);
    }

}
